import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution874 {
    public int robotSim(int[] commands, int[][] obstacles) {
        int x=0,y=0,d=0,ans=0;
        int[] dx=new int[]{0,1,0,-1};
        int[] dy=new int[]{1,0,-1,0};
        Set<List<Integer>> set=new HashSet<>();
        for (int[] obstacle : obstacles) {
            set.add(Arrays.asList(obstacle[0],obstacle[1]));
        }
        for (int command : commands) {
            if (command==-1){
                d=(d+1)%4;
            }else if (command==-2){
                d=(d+3)%4;
            }else{
                for (int i=0;i<command;i++){
                    int xx=x+dx[d];
                    int yy=y+dy[d];
                    if (set.contains(Arrays.asList(xx,yy))){
                        break;
                    }
                    x=xx;
                    y=yy;
                }
            }
            ans=Math.max(ans,x*x+y*y);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution874().robotSim(new int[]{4,-1,4,-2,4},new int[][]{{2,4}}));
    }
}
